Boss = class("Boss", HPlane)



function Boss:ctor(hp, speed, fireRate, tag, prefab)
    --飞行速度
    self.flySpeed = speed;
    --移动方向
    self.direction = Vector3.zero;
    --状态改变时间
    self.lastTime = 0;
    --状态
    self.EnemyStateTab = {{"Enter", "Idle","Move","Attack","Change","MoveAttack"} ,
                          {"Change", "MoveAttack","Idle","Move","Attack"},
                          {"Idle", "MoveAttack","MoveAttack","Move","Attack", "Idle"},
                         }
    self.EnemyState = "Enter";
    self.EnemyStateTabIndex = 1;
    self.EnemyStateIndex = 1;

    --初始化属性
    self.flag = 1;
    self.interval = 0
    self.lastFireTime = 0;
    self.index = 1;

    --血量
    self.hpGo = newObject(ThunderManager.prefabs[8]);
    self.hpGo.transform:SetParent(ThunderManager.canvas, false)
    coroutine.start(self.DestroyAnim, self);
    self.hpGoImg1 = self.hpGo.transform:GetChild(0):GetComponent('Image');
    self.hpGoImg2 = self.hpGo.transform:GetChild(1):GetComponent('Image');

    --获取屏幕范围
    local screenMax = Camera.main:ScreenToWorldPoint(Vector3(Screen.width, Screen.height));
    self.maxX = screenMax.x;
    self.maxY = screenMax.y;

    self.super.ctor(self, hp, speed, fireRate, tag, prefab)

    --获取发射口
    self.firePoints= {};
    for i=1,self.transform.childCount do
        self.Points = {};
        for j=1,self.transform:GetChild(i - 1).childCount do
            
            table.insert(self.Points, self.transform:GetChild(i - 1):GetChild(j - 1))
        end

        table.insert(self.firePoints, self.Points)
    end

end
function Boss:DestroyAnim()
    coroutine.wait(1);
    destroy(self.hpGo:GetComponent('Animator'));
end

function Boss:Update()
   self:Move();
end

function Boss:Move()
   if self.EnemyState == "Enter" then
       self:OnEnter();
    elseif self.EnemyState== "Idle" then
       self:OnIdle();
        
    elseif self.EnemyState== "Move" then
        self:OnMove();

    elseif self.EnemyState== "Attack" then
        self:OnAttack();

    elseif self.EnemyState== "Change" then
        self:OnChange();

    elseif self.EnemyState== "MoveAttack" then
        self:OnMoveAttack();
          
   end
end

function Boss:OnEnter()
    
    self.transform:Translate(Vector3.down * self.flySpeed * Time.deltaTime);
    if self.transform.position.y <= 3  then
        
         --计算移动方向
        if (self.transform.position.x < 0) then
            self.direction = Vector3.right;
        else
            self.direction = Vector3.left;
        end
        self:SwitcherToNext();
    end
end

function Boss:OnIdle()
    
    if(Time.time- self.lastTime > 2) then
        
        self:SwitcherToNext();

    end
end

function Boss:OnMove()
    
    self.transform:Translate(self.direction * self.flySpeed * Time.deltaTime);
    if (self.transform.position.x >= 1.3 or self.transform.position.x <= -1.3) then
        
        if (self.transform.position.x < 0) then
        self.direction = Vector3.right;
        else
             self.direction = Vector3.left;
        end
        self:SwitcherToNext();

    end
end

function Boss:OnAttack()
    
    self:Fire();
    if (Time.time-self.lastTime> 1) then      
        self:SwitcherToNext();

    end
end

function Boss:OnChange()
    
    self:NextGun();
    self:SwitcherToNext();
    
end

--左右移动，释放子弹
function Boss:OnMoveAttack()
    
    
    self.transform:Translate(Vector3.right * self.flag * self.flySpeed * Time.deltaTime);
    if (self.transform.position.x >= 1) then
        
        self.flag = -1;
    end
    if(self.transform.position.x <= -1) then
        
        self.flag = 1;
    end
    
    self:Fire();
    
    if (Time.time - self.lastTime > 1) then 
        
    self:SwitcherToNext();
       

    end
end

--切换状态
function Boss:SwitcherToNext()
    --如果当前状态超出当前状态表的长度，切换到下一个状态表
    --如果超出总表长度则从新开始
    self.EnemyStateIndex = self.EnemyStateIndex + 1
    if self.EnemyStateIndex > #(self.EnemyStateTab[self.EnemyStateTabIndex]) then
        self.EnemyStateIndex = 1;
        self.EnemyStateTabIndex = self.EnemyStateTabIndex + 1;

        if self.EnemyStateTabIndex > 3 then
            self.EnemyStateTabIndex = 1;
        end

    end

self.EnemyState = self.EnemyStateTab[self.EnemyStateTabIndex][self.EnemyStateIndex];
self.lastTime = Time.time;

end

function Boss:Fire()
  
    if Time.time - self.lastFireTime > self.fireInterval then
        self.lastFireTime = Time.time;
        for i=1,#(self.firePoints[self.index]) do           

            Bullet.new(self.firePoints[self.index][i],5,"EnemyBullet",ThunderManager.prefabs[7])
        end
    end

end


function Boss:NextGun()
    self.index = self.index + 1;
    if self.index > #(self.firePoints) then
        self.index = 1;
    end
end


function Boss:OnTriggerEnter2D(collider)
    self.super.OnTriggerEnter2D(self, collider);

        if  collider.gameObject:CompareTag("PlayerBullet") then
            destroy(collider.gameObject)
            
            self:SubHp()              

        elseif collider.gameObject:CompareTag("Player") then
                  
        end

end

function Boss:SubHp()
    
    self.super.SubHp(self)
    
    self.num = self.hp - self.maxHp / 2 ;
    if self.num >= 0 then
        self.hpGoImg2.fillAmount = self.num / (self.maxHp / 2);
        
    else
        self.hpGoImg1.fillAmount = self.hp / (self.maxHp / 2);
    end

    if self:IsDath() then
        ThunderManager.AddScore(10);
        destroy(self.hpGo);
    end
end
